//
//  WJIconFontUtil.m
//  imfei
//
//  Created by 王军 on 2017/9/30.
//  Copyright © 2017年 jun.wang. All rights reserved.
//
/***
 使用方法：
 1.找到icon的ttf文件，拖入项目。
 2.确认资源是否被加载
 3.在plist文件里面， 增加 Fonts provided by application，并输入ttf文件名。
 
 使用：
 UIImage *icon = [UIImage imageWithIcon:@"fa-github" backgroundColor:[UIColor purpleColor] iconColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:255] fontSize:50];
 
     icon = [UIImage imageWithIcon:@"fa-bitbucket" backgroundColor:[UIColor colorWithRed:0.1 green:0.3 blue:0.5 alpha:255] iconColor:[UIColor whiteColor] fontSize:100];
 
 
 备注信息：
 ttf文件编辑器：https://www.fontlab.com/font-editor/fontlab-studio/
 github:https://github.com/alexdrone/ios-fontawesome
 
 
 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 300, 50)];
 label.font = [UIFont fontWithName:@"fontello" size:35];
 label.text = @"\U0000E802 \U0000E801 \U0000E803 \U0000E804 \U0000E805 \U0000E81A";
 label.textColor = UIColorFromRGB(0xFF7300);
 [self.view addSubview:label];
 
 
 ***/


#import "WJIconFontUtil.h"

@implementation WJIconFontUtil

+(void)printAllFonts{
    for (NSString* family in [UIFont familyNames]){
        NSLog(@"%@", family);
        for (NSString* name in [UIFont fontNamesForFamilyName: family]){
            NSLog(@"  %@", name);
        }
    }
}

+(UIImage *)getImgae:(NSString *)fontName code:(NSString *)code fontSize:(CGFloat)fontSize bgColor:(UIColor *)bgColor  iconColor:(UIColor *)iconColor{
    if (!bgColor) {
        bgColor = [UIColor clearColor];
    }
    if (!iconColor) {
        iconColor = [UIColor whiteColor];
    }
    //// Abstracted Attributes
    NSString* textContent = code;
    UIFont *font = [UIFont fontWithName:fontName size:fontSize];
    if (font == nil) {
        [WJAlertUtils alertMsg:@"查看字体是否加入plist" vc:nil];
        return [WJCommonUtils imageFromColor:[UIColor whiteColor]];
    }
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{NSFontAttributeName : font,
                                 NSForegroundColorAttributeName : iconColor,
                                 NSBackgroundColorAttributeName : bgColor,
                                 NSParagraphStyleAttributeName: style,
                                 };
    //// Content Edge Insets
    CGSize size = [textContent sizeWithAttributes:attributes];
    size = CGSizeMake(size.width * 1.1, size.height * 1.05);
    CGRect textRect = CGRectZero;
    textRect.size = size;
    CGPoint origin = CGPointMake(size.width * 0.05, size.height * 0.025);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
    //// Rectangle Drawing
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:textRect];
    [bgColor setFill];
    [path fill];
    //// Text Drawing
    [textContent drawAtPoint:origin withAttributes:attributes];
    //Image returns
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+(UIImage *)getImage:(NSString *)name fontSize:(CGFloat)fontSize bgColor:(UIColor *)bgColor  iconColor:(UIColor *)iconColor{
    NSArray *arr = @[
                     @[@"首页",@"\U0000E8D8"],
                     @[@"首页_空",@"\U0000E8D7"],
                     @[@"我",@"\U0000E8F6"],
                     @[@"我_空",@"\U0000E8F6"],
                     @[@"商城",@"\U0000E600"],
                     @[@"关于产品",@"\U0000E8B2"],
                     @[@"答疑解惑",@"\U0000E92B"],
                     @[@"关于我们",@"\U0000E931"],
                     @[@"隐私申明",@"\U0000E8E3"],
                     @[@"安装说明",@"\U0000E8A1"],
                     @[@"温度设定",@"\U0000E627"],
                     @[@"童锁",@"\U0000E700"],
                     @[@"睡眠模式",@"\U0000E622"],
                     @[@"定时开关",@"\U0000E614"],
                     @[@"设备管理",@"\U0000E91B"],
                     @[@"电源开",@"\U0000E695"],
                     @[@"电源关",@"\U0000E695"],
                     
                     ];
    
    NSString *code = @"";
    for (NSArray * a in arr) {
        if ([a[0] isEqualToString:name]) {
            code = a[1];
            break;
        }
    }
    return [WJIconFontUtil getImgae:@"iconfont" code:code fontSize:fontSize bgColor:bgColor iconColor:iconColor];
}

// 根据名称获取图片
+(UIImage *)getDefaultImage:(NSString *)name{
    if ([name isEqualToString:@"电源开"]) {
        return [WJIconFontUtil getImage:name fontSize:32 bgColor:[UIColor clearColor] iconColor:[UIColor redColor]];
    }
    if ([name isEqualToString:@"电源关"]) {
        return [WJIconFontUtil getImage:name fontSize:32 bgColor:[UIColor clearColor] iconColor:[UIColor whiteColor]];
    }
    
//    @[@"电源开",@"\U0000E60C"],
//    @[@"电源关",@"\U0000E60C"],
    return [WJIconFontUtil getImage:name fontSize:64 bgColor:[UIColor clearColor] iconColor:COLOR_COMMON_ONE];
}



@end
